package com.zkingsoft.actions.visitor;

import java.util.Date;
import java.util.LinkedList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tencent.common.RandomStringGenerator;
import com.tencent.common.Signature;
import com.tencent.protocol.simple_protocol.SimpleProtol;
import com.tencent.service.CommonService;
import com.zkingsoft.actions.common.BaseController;
import com.zkingsoft.constance.Dictionary;
import com.zkingsoft.constance.SystemConstance;
import com.zkingsoft.model.AdvertisementPosition;
import com.zkingsoft.model.FlowCz;
import com.zkingsoft.model.FlowLookFy;
import com.zkingsoft.model.Fy;
import com.zkingsoft.plugin.pojo.PaginationVO;
import com.zkingsoft.plugin.util.DateUtils;
import com.zkingsoft.plugin.util.LogUtil;
import com.zkingsoft.plugin.util.WebUtil;
import com.zkingsoft.pojo.AjaxResult;
import com.zkingsoft.services.AdvertisementColumnService;
import com.zkingsoft.services.AdvertisementPositionService;
import com.zkingsoft.services.FlowCzService;
import com.zkingsoft.services.FlowLookFyService;
import com.zkingsoft.services.FyService;
import com.zkingsoft.services.UsersService;

/**
 * 
 * @Description: 不需要登录可以访问的action
 * @author:姜友瑶
 * @date 2016年8月31日
 */
@Controller
@RequestMapping(value = "/index")
public class IndexController extends BaseController {

	@Resource
	private UsersService userService;

	@Resource
	private FyService fyService;

	@Resource
	FlowLookFyService flowLookFyService;

	@Resource
	FlowCzService flowCzService;

	@Resource
	private AdvertisementPositionService adPositionService;
	@Resource
	private AdvertisementColumnService advertisementColumnService;

	/**
	 * 
	 * @Description: 查询房源列表
	 * @author:姜友瑶
	 * @return 返回类型 AjaxResult
	 * @date 2016年10月11日
	 */
	@RequestMapping(value = "/queryFy")
	public @ResponseBody AjaxResult queryFy(Fy fy, PaginationVO pageVo, String account, String price) {
		if (account != null && !"".equals(account)) {
			fy.setFyImage(account.replaceAll("-", " and "));
		}
		if (price != null && !"".equals(price)) {
			fy.setFyLxr(price.replaceAll("-", " and "));
		}
		fy.setGlPublishStatus("上架");
		fy.setYxzt("有效");
		fy.setGlCheckStatus("审核通过");
		List<Fy> fyList = fyService.findInPage(fy, pageVo);
		return new AjaxResult(AjaxResult.STATUS_OK, null, "查询成功", fyList, fyService.findTotal(fy));
	}

	/**
	 * 
	 * @Description: 查询房源信息
	 * @author:姜友瑶
	 * @return 返回类型 AjaxResult
	 * @date 2016年10月11日
	 */
	@RequestMapping(value = "/queryFyMsg")
	public String queryFyMsg(Long id) {

		// 判断是否进行了自动登录，如果是微信则自动登录
		if (WebUtil.isWeiXin() && WebUtil.getSession().getAttribute(Dictionary.AUTO_LOGINED) == null) {
			LogUtil.info(this.getClass(), "callback url= " + Dictionary.WXAUTODL_CALLBACK);
			WebUtil.getSession().setAttribute("callback", WebUtil.getCurrentPageUrl());
			return "redirect:" + SimpleProtol.getUserSq(Dictionary.WXAUTODL_CALLBACK, "snsapi_base",
					"" + System.currentTimeMillis());
		}

		List<AdvertisementPosition> fyggList = adPositionService.findPointByCloumName(Dictionary.FYXXGG);
		if (fyggList.size() == 1) {
			WebUtil.getRequest().setAttribute("fygg", fyggList.get(0));
		}
		
		//未登陆
		if (getMe() == null) {
			return "visitor/tologin";
//			WebUtil.getRequest().setAttribute("obj", fyService.findById(id));
//			WebUtil.getRequest().setAttribute("looked", "否");
//			return "visitor/house-msg";
		} else {
			//已经登录
			if (WebUtil.isWeiXin()) {
				// 获取微信分享需要的参数
				String timestamp = DateUtils.getTimeSecent();
				String noncestr = RandomStringGenerator.getRandomStringByLength(32);
				// 微信分享链接必须要和当前网页的连接保持一致且不能带端口号包括80【这是一个巨大的坑】
				String url = WebUtil.getCurrentPageUrl();
				String sign = null;
				try {
					sign = Signature.getJsSdkSign(CommonService.getJsapiTicket(), noncestr, timestamp, url);
				} catch (Exception e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}

				WebUtil.getRequest().setAttribute("timestamp", timestamp);
				WebUtil.getRequest().setAttribute("noncestr", noncestr);
				WebUtil.getRequest().setAttribute("signature", sign);
				WebUtil.getRequest().setAttribute("url", url);
			}
			// 添加用户访问记录
			FlowLookFy flowLookFy = new FlowLookFy(getMe().getId(), id, new Date());
			flowLookFyService.add(flowLookFy);
		Fy fy=	 fyService.findById(id);
			//判断是否为自己的房源
			if(fy.getGlUserId()!=null && fy.getGlUserId().equals(getMe().getId())){
				WebUtil.getRequest().setAttribute("looked", "是");
			}else{
				// 查询用户是否已经查看过改房源
				FlowCz flowCz = new FlowCz();
				flowCz.setUserId(getMe().getId());
				flowCz.setTargetId(id);
				List<FlowCz> list = flowCzService.findByModel(flowCz);
				if (list != null && list.size() > 0) {
					WebUtil.getRequest().setAttribute("looked", "是");
				} else {
					WebUtil.getRequest().setAttribute("looked", "否");
				}
			}
			
			WebUtil.getRequest().setAttribute("obj", fy);
			return "visitor/house-msg";
		}

	}

	/**
	 * 
	 * @Description: 查询电话号码
	 * @author:姜友瑶
	 * @return 返回类型 AjaxResult
	 * @date 2016年10月11日
	 */
	@RequestMapping(value = "/queryTel")
	public @ResponseBody AjaxResult queryTel(Long id) {
		// 添加用户访问记录
		FlowCz flowCz = new FlowCz(getMe().getId(), id, Dictionary.JBZC, new Date(), "查看房源消耗金币");
		flowCzService.userLookFy(flowCz);
		// 更新用户金币
		WebUtil.getSession().setAttribute(SystemConstance.LOGIN_KEY, userService.findById(getMe().getId()));
		return new AjaxResult(AjaxResult.STATUS_OK, "", fyService.findById(id).getFyLxdh());
	}

	/**
	 * 查询房源
	 * 
	 * @date 2016-06-24
	 */
	@RequestMapping(value = "/searchFy")
	public String searchFy(String keyWord) {

		WebUtil.getRequest().setAttribute("keyWord", keyWord);
		;
		if (keyWord != null) {

			LinkedList<String> searchList = null;

			// 获取最近查询标题
			if (WebUtil.getSession().getAttribute(SystemConstance.HISTORY_SEARCH) == null) {
				searchList = new LinkedList<String>();
				WebUtil.getSession().setAttribute(SystemConstance.HISTORY_SEARCH, searchList);
			} else {
				searchList = (LinkedList<String>) WebUtil.getSession().getAttribute(SystemConstance.HISTORY_SEARCH);
			}
			// 满20后删除一个
			if (searchList.size() == 20) {
				searchList.poll();
			}
			// 去重标志
			boolean isNoRepeat = true;
			for (String string : searchList) {

				if (string.equals(keyWord)) {
					isNoRepeat = false;
				}
			}
			if (isNoRepeat) {
				searchList.add(keyWord);
			}
		}

		return "visitor/search-list";
	}
}